package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.Teacher;
import com.grad.why.studentsystem.domain.current.CurrentTeacher;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OptionparamsService optionparamsService;

    @RequestMapping("/getTeacherInfoList")
    @ResponseBody
    public Object getStudentList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<CurrentTeacher> rows = teacherService.getTeacherList(offset, limit, sort, search);
            long total = teacherService.getTeacherListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteTeacher/{ids}")
    @ResponseBody
    public String deleteTeacherByIds(@PathVariable("ids") String ids){
        try{
            String[] idArray = ids.split("-");
            for(String s : idArray){
                int rows = teacherService.deleteTeacherById(Integer.parseInt(s));
                if(rows <= 0){
                    return "FAILED";
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "FAILED";
    }

    @RequestMapping("/addTeacher")
    @ResponseBody
    public Teacher addTeacher(Teacher teacher,String transbirthday){
        try{
            Integer tId = teacherService.getMaxId() + 1;
            teacher.setId(tId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            teacher.setBirthday(sdf.parse(transbirthday));
            return teacherService.saveTeacherByEntity(teacher);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateTeacher")
    @ResponseBody
    public Teacher updateTeacher(Teacher teacher,String transbirthday){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            teacher.setBirthday(sdf.parse(transbirthday));

            int rows = teacherService.updateTeacherByEntity(teacher);

            if(rows > 0){
                return teacher;
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getTeacherInfoByCourseId/{cid}")
    @ResponseBody
    public String getTeacherInfoByCourseId(@PathVariable("cid") String cid){
        try{
            Teacher teacher = teacherService.getTeacherInfoByCourseId(Integer.parseInt(cid));
            if(teacher!=null){
                return teacher.getName();
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadTeacherInfos")
    @ResponseBody
    public String uploadTeacherInfos(@RequestParam("file") MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadTeacherInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadTeacherInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = teacherService.addTeacherInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
